package com.xiang.activity;

import com.xiang.adapter.GetRegionAdapter;
import com.xiang.constant.RegionsBaseinfo;
import com.xiang.entity.GetRegionsEntity;
import com.xiang.seemovies.R;
import com.xiang.utils.JsonUtil;
import com.xiang.utils.sharedpreferencesUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cn.edu.fzxy.zxy.niceday.AsyncHttpGet;
import cn.edu.fzxy.zxy.niceday.DefaultThreadPool;
import cn.edu.fzxy.zxy.niceday.RequestResultCallback;
import cn.edu.fzxy.zxy.niceday.examples.BaseActivity;

public class GetRegionsActivity extends BaseActivity {
	public String city = "深圳";

	private RegionsBaseinfo info;

	private ListView listview;
	
	private TextView city_tittle;

	private GetRegionAdapter adapter;

	private int show_list = 10000;

	private MyHandler handler;
	
	private ImageView back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regions_layout);
		initData();
		initView();
		request();
	}

	private void initData() {
		String city_name = getIntent().getStringExtra("city_name");
		if (city_name != null && !(city_name.equals(""))) {
			city = city_name;
		}
		if (handler == null) {
			handler = new MyHandler();
		}
	}

	private void initView() {
		listview = (ListView) findViewById(R.id.listview_regios_list);
		listview.setOnItemClickListener(new ItemClickListener());
		city_tittle = (TextView) findViewById(R.id.regions_tittle);
		city_tittle.setText(city);
		back = (ImageView) findViewById(R.id.get_regions_back);
		back.setOnClickListener(new ImageView.OnClickListener() {
			@Override
			public void onClick(View v) {
				GetRegionsActivity.this.finish();
				overridePendingTransition(R.anim.left_in, R.anim.right_out);
			}
		});
	}

	class ItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			sharedpreferencesUtil util = new sharedpreferencesUtil(GetRegionsActivity.this);
			util.saveCity(city);
			util.saveRegion(info.districtsList.get(arg2).district_name);
			showListview();
		}

	}

	private void showListview() {
		if (adapter == null) {
			adapter = new GetRegionAdapter(this, info.districtsList);
			listview.setAdapter(adapter);
		} else {
			adapter.resetData(info.districtsList);
			adapter.notifyDataSetChanged();
		}
	}

	private void request() {
		GetRegionsEntity entity = new GetRegionsEntity(city);
		AsyncHttpGet get = new AsyncHttpGet(null, entity.getUrl(),
				entity.getParameterList(), new RequestResultCallback() {

					@Override
					public void onSuccess(Object o) {
						System.out.println("#########result: " + o.toString());
						info = JsonUtil.analysisRegions(o.toString());
						Message msg = new Message();
						msg.what = show_list;
						handler.sendMessage(msg);
					}

					@Override
					public void onFail(Exception e) {
						System.out.println("#########Exception");
					}
				});
		DefaultThreadPool.getInstance().execute(get);
		this.requestList.add(get);
	}

	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == show_list) {
				showListview();
			}
		}
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                 && event.getRepeatCount() == 0) {
        	GetRegionsActivity.this.finish();
        	overridePendingTransition(R.anim.left_in, R.anim.right_out);
             return true;
         }
         return super.onKeyDown(keyCode, event);
     }

}
